<?php

namespace App\Http\Controllers\Web\Shop;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\Products;
use App\Repository\Models\Shop;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Exception;

/**
 * 商品管理控制器
 *
 * @package App\Http\Controllers\Web\Seller
 */
class ProductController extends BaseController
{

    protected $product_repo;
    protected $product_sku_repo;
    protected $id;


    public function __construct(ProductsRepository $product_repo,ProductSkusRepository $product_sku_repo)
    {
        $this->product_repo = $product_repo;
        $this->product_sku_repo = $product_sku_repo;

    }

    public function index(Request $request,$id)
    {
        $this->id=$id;
        $product = $this->product_repo->with(['skus','categories','brand'])->scopeQuery(function ($query) {
            return $query->whereIn('shop_id', [$this->id])->whereIn('is_for_sale',[Products::IS_FOR_SALE_YES])->whereIn('status',[Products::STATUS_CHECKED])->where('product_type',Products::IS_NORMAL_TYPE);
        })->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product, true);
    }

    public function shop_product(Request $request,$id)
    {
        $this->id=$id;
        $product = $this->product_repo->with(['skus','categories','brand'])->scopeQuery(function ($query) {
            return $query->whereIn('shop_id', [$this->id])->whereIn('is_for_sale',[Products::IS_FOR_SALE_YES])->whereIn('status',[Products::STATUS_CHECKED]);
        })->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product, true);
    }


    public function show($id)
    {
        $product = $this->product_repo->with('skus')->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
    }

    public function get_third_cat($id){
        $this->id=$id;

        $product = $this->product_repo->with(['categories','brand'])->scopeQuery(function ($query) {
                    return $query->whereIn('shop_id', [$this->id]);
                })->paginate($this->perPage,['id','third_cat','brand_id']);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data'],true);

    }

    public function attribute(Request $request,$id)
    {
      $this->id=$id;
      $product = $this->product_repo->with(['attrs','categories'])->scopeQuery(function ($query) {
          return $query->whereIn('shop_id', [$this->id])->whereIn('is_for_sale',[Products::IS_FOR_SALE_YES])->whereIn('status',[Products::STATUS_CHECKED])->where('product_type',Products::IS_QUALIFIED_TYPE);
      })->paginate($this->perPage);
      return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product, true);
    }

}
